<template>
    <Tabs type="card" @on-click="searchData(tabIndex)" v-model="tabIndex">
      <TabPane label="监测时限">
        <Form ref="addBaseForm" :label-width="80">
          <div v-for="(item, index) in timeAreaData" v-bind:key="item.company_code">
            <FormItem :label="item.company_name">
              <Row>
                <Col span="3">
                  <FormItem prop="frequency">
                    <Input v-model="item.day" type="number" placeholder="填写天数"></Input>
                  </FormItem>
                </Col>
                <Col span="1" style="text-align: center">天</Col>
                <Col span="3" offset="1">
                  <FormItem>
                    <Button type="primary" @click="updatesupervisedayinfo(item, index, tabIndex)">保存</Button>
                  </FormItem>
                </Col>
              </Row>
            </FormItem>
          </div>
        </Form>
      </TabPane>
      <TabPane label="体温监测时间">
        <Form ref="addBaseForm" :label-width="80">
          <div v-for="(item, index) in areaData" v-bind:key="item.label">
            <FormItem :label="item.label">
              <Row>
                <Col span="4">
                  <FormItem>
                    <RadioGroup v-model="item.mode" @on-change="changeMode(item)">
                      <Radio label="1">n天n次</Radio>
                      <Radio label="2">第n天1次</Radio>
                  </RadioGroup>
                  </FormItem>
                </Col>
                <Col span="2">
                  <FormItem prop="day">
                    <Input v-model="item.frequency" type="number" :disabled="item.mode == '2'" placeholder="填写次数"></Input>
                  </FormItem>
                </Col>
                <Col span="1" style="text-align: center">/</Col>
                <Col span="2">
                  <FormItem prop="frequency">
                    <Input v-model="item.day" type="text" placeholder="填写天数"></Input>
                  </FormItem>
                </Col>
                <Col span="2" offset="1">
                  <FormItem>
                    <Button type="primary" @click="savetime(item, index, tabIndex)">保存</Button>
                  </FormItem>
                </Col>
                <Col span="7">
                  <FormItem :label-width="400" label="当选择”第n天一次“类型时，天数可以传逗号分隔的天数形式:如1,3,5,7">
                  </FormItem>
                </Col>
              </Row>
            </FormItem>
          </div>
        </Form>
      </TabPane>
      <TabPane label="核酸监测时间">
        <Form ref="addBaseForm" :label-width="80">
          <div v-for="(item, index) in areaData" v-bind:key="item.label">
            <FormItem :label="item.label">
              <Row>
                <Col span="4">
                  <FormItem>
                    <RadioGroup v-model="item.mode" @on-change="changeMode(item)">
                      <Radio label="1">n天n次</Radio>
                      <Radio label="2">第n天1次</Radio>
                  </RadioGroup>
                  </FormItem>
                </Col>
                <Col span="2">
                  <FormItem prop="day">
                    <Input v-model="item.frequency" type="number" :disabled="item.mode == '2'" placeholder="填写次数"></Input>
                  </FormItem>
                </Col>
                <Col span="1" style="text-align: center">/</Col>
                <Col span="2">
                  <FormItem prop="frequency">
                    <Input v-model="item.day" type="text" placeholder="填写天数"></Input>
                  </FormItem>
                </Col>
                <Col span="2" offset="1">
                  <FormItem>
                    <Button type="primary" @click="savetime(item, index, tabIndex)">保存</Button>
                  </FormItem>
                </Col>
                <Col span="7">
                  <FormItem :label-width="400" label="当选择”第n天一次“类型时，天数可以传逗号分隔的天数形式:如1,3,5,7">
                  </FormItem>
                </Col>
              </Row>
            </FormItem>
          </div>
        </Form>
      </TabPane>
      <TabPane label="血清监测时间">
        <Form ref="addBaseForm" :label-width="80">
          <div v-for="(item, index) in areaData" v-bind:key="item.label">
            <FormItem :label="item.label">
              <Row>
                <Col span="4">
                  <FormItem>
                    <RadioGroup v-model="item.mode" @on-change="changeMode(item)">
                      <Radio label="1">n天n次</Radio>
                      <Radio label="2">第n天1次</Radio>
                  </RadioGroup>
                  </FormItem>
                </Col>
                <Col span="2">
                  <FormItem prop="day">
                    <Input v-model="item.frequency" type="number" :disabled="item.mode == '2'" placeholder="填写次数"></Input>
                  </FormItem>
                </Col>
                <Col span="1" style="text-align: center">/</Col>
                <Col span="2">
                  <FormItem prop="frequency">
                    <Input v-model="item.day" type="text" placeholder="填写天数"></Input>
                  </FormItem>
                </Col>
                <Col span="2" offset="1">
                  <FormItem>
                    <Button type="primary" @click="savetime(item, index, tabIndex)">保存</Button>
                  </FormItem>
                </Col>
                <Col span="7">
                  <FormItem :label-width="400" label="当选择”第n天一次“类型时，天数可以传逗号分隔的天数形式:如1,3,5,7">
                  </FormItem>
                </Col>
              </Row>
            </FormItem>
          </div>
        </Form>
      </TabPane>
    </Tabs>
</template>
<script>
import validateData from './validate'
import { saveRule, findRuleList, findSupervisedayList, saveSuperviseday } from '@/api/yi-qing-fang-kong'
export default {
  name: 'time-limit-setting',
  data () {
    return {
      tabIndex: '',
      formValidate: {},
      ruleValidate: validateData.ruleValidate,
      timeAreaData: [
        {
          company_name: '沈阳市',
          day: '',
          id: '',
          company_code: 'LM'
        },
        {
          company_name: '和平区',
          day: '',
          id: '',
          company_code: 'LM00001'
        },
        {
          company_name: '沈河区',
          day: '',
          id: '',
          company_code: 'LM00002'
        },
        {
          company_name: '铁西区',
          day: '',
          id: '',
          company_code: 'LM00005'
        },
        {
          company_name: '皇姑区',
          day: '',
          id: '',
          company_code: 'LM00004'
        },
        {
          company_name: '大东区',
          day: '',
          id: '',
          company_code: 'LM00003'
        },
        {
          company_name: '浑南区',
          day: '',
          id: '',
          company_code: 'LM00006'
        },
        {
          company_name: '于洪区',
          day: '',
          id: '',
          company_code: 'LM00007'
        },
        {
          company_name: '沈北新区',
          day: '',
          id: '',
          company_code: 'LM00008'
        },
        {
          company_name: '苏家屯区',
          day: '',
          id: '',
          company_code: 'LM00009'
        },
        {
          company_name: '新民市',
          day: '',
          id: '',
          company_code: 'LM00013'
        },
        {
          company_name: '辽中区',
          day: '',
          id: '',
          company_code: 'LM00010'
        },
        {
          company_name: '法库县',
          day: '',
          id: '',
          company_code: 'LM00012'
        },
        {
          company_name: '康平县',
          day: '',
          id: '',
          company_code: 'LM00011'
        }
      ],
      areaData: [
        {
          label: '和平区',
          day: '',
          frequency: '',
          qucode: 'LM00001',
          mode: '1'
        },
        {
          label: '沈河区',
          day: '',
          frequency: '',
          qucode: 'LM00002',
          mode: '1'
        },
        {
          label: '铁西区',
          day: '',
          frequency: '',
          qucode: 'LM00005',
          mode: '1'
        },
        {
          label: '皇姑区',
          day: '',
          frequency: '',
          qucode: 'LM00004',
          mode: '1'
        },
        {
          label: '大东区',
          day: '',
          frequency: '',
          qucode: 'LM00003',
          mode: '1'
        },
        {
          label: '浑南区',
          day: '',
          frequency: '',
          qucode: 'LM00006',
          mode: '1'
        },
        {
          label: '于洪区',
          day: '',
          frequency: '',
          qucode: 'LM00007',
          mode: '1'
        },
        {
          label: '沈北新区',
          day: '',
          frequency: '',
          qucode: 'LM00008',
          mode: '1'
        },
        {
          label: '苏家屯区',
          day: '',
          frequency: '',
          qucode: 'LM00009',
          mode: '1'
        },
        {
          label: '新民市',
          day: '',
          frequency: '',
          qucode: 'LM00013',
          mode: '1'
        },
        {
          label: '辽中区',
          day: '',
          frequency: '',
          qucode: 'LM00010',
          mode: '1'
        },
        {
          label: '法库县',
          day: '',
          frequency: '',
          qucode: 'LM00012',
          mode: '1'
        },
        {
          label: '康平县',
          day: '',
          frequency: '',
          qucode: 'LM00011',
          mode: '1'
        }
      ],
      temp_number: 0
    }
  },
  watch: {
  },
  methods: {
    changeMode (item) {
      if (item.mode === '2') {
        item.frequency = '1'
      } else if (item.mode === '1') {
        item.frequency = ''
      }
    },
    searchData (index) { // 切换tab页
      if (index === 0) {
        this.getsuperviseday()
      } else {
        this.init(index)
      }
    },
    savetime (item, index, tabIndex) {
      if (!item.frequency || item.frequency === '') {
        const content = '<p>请填写次数</p>'
        const errorTitle = '错误提示'
        this.$Modal.error({
          title: errorTitle,
          content: content
        })
        return
      }
      if (!item.day || item.day === '') {
        const content = '<p>请填写天数</p>'
        const errorTitle = '错误提示'
        this.$Modal.error({
          title: errorTitle,
          content: content
        })
        return
      }
      this.temp_number = index
      let param = {
        type: this.tabIndex,
        mode: item.mode,
        day: item.day,
        frequency: item.frequency,
        qucode: item.qucode
      }
      console.log(param, '入参')
      saveRule(param).then(res => {
        if (res.data.errcode === 0) {
          this.$Notice.success({ title: '设置成功' })
          this.tabIndex = tabIndex
          this.init(this.tabIndex)
        } else {
          this.$Notice.error({ title: '设置失败' })
        }
      })
    },
    init (type) { // findRuleList
      this.areaData = [ { label: '和平区', day: '', frequency: '', qucode: 'LM00001', mode: '1' }, { label: '沈河区', day: '', frequency: '', qucode: 'LM00002', mode: '1' },
        { label: '铁西区', day: '', frequency: '', qucode: 'LM00005', mode: '1' }, { label: '皇姑区', day: '', frequency: '', qucode: 'LM00004', mode: '1' },
        { label: '大东区', day: '', frequency: '', qucode: 'LM00003', mode: '1' }, { label: '浑南区', day: '', frequency: '', qucode: 'LM00006', mode: '1' }, { label: '于洪区', day: '', frequency: '', qucode: 'LM00007', mode: '1' }, { label: '沈北新区', day: '', frequency: '', qucode: 'LM00008', mode: '1' },
        { label: '苏家屯区', day: '', frequency: '', qucode: 'LM00009', mode: '1' }, { label: '新民市', day: '', frequency: '', qucode: 'LM00013', mode: '1' }, { label: '辽中区', day: '', frequency: '', qucode: 'LM00010', mode: '1' }, { label: '法库县', day: '', frequency: '', qucode: 'LM00012', mode: '1' },
        { label: '康平县', day: '', frequency: '', qucode: 'LM00011', mode: '1' } ]
      let param = {
        type: type
      }
      findRuleList(param).then(res => {
        if (res.data.errcode === 0) {
          for (var i = 0; i < this.areaData.length; i++) {
            for (var j = 0; j < res.data.data.length; j++) {
              if (this.areaData[i].qucode === res.data.data[j].qucode) {
                this.areaData[i].label = this.areaData[i].label
                this.areaData[i].mode = res.data.data[j].mode
                this.areaData[i].day = res.data.data[j].day
                this.areaData[i].frequency = res.data.data[j].frequency
                this.areaData[i].qucode = res.data.data[j].qucode
                // return
              }
            }
          }
        } else {
          this.$Notice.error({ title: res.data.errmsg })
        }
      })
    },
    getsuperviseday () {
      findSupervisedayList(this.params).then(res => {
        if (res.data.errcode === 0) {
          for (var i = 0; i < this.timeAreaData.length; i++) {
            for (var j = 0; j < res.data.data.length; j++) {
              if (this.timeAreaData[i].company_code === res.data.data[j].company_code) {
                this.timeAreaData[i].company_name = this.timeAreaData[i].company_name
                this.timeAreaData[i].day = res.data.data[j].day
              }
            }
          }
        } else {
          this.loadData = true
          this.$Notice.error({
            title: '查询列表信息失败',
            desc: res.data.errmsg
          })
        }
      })
      // this.supervisedayinfo.isshow = true
    },
    updatesupervisedayinfo (param, index, tabIndex) {
      saveSuperviseday(param).then(res => {
        if (res.data.errcode === 0) {
          this.$Notice.success({ title: '设置成功' })
          this.getsuperviseday()
          this.tabIndex = tabIndex
        } else {
          this.$Notice.error({ title: '保存失败' })
        }
      })
    }
  },
  mounted () {
    this.init('1')
    this.getsuperviseday()
  }
}
</script>
